#include "QPropertyDelegate.h"
#include "QProperty.h"

#include <QApplication>
#include <QStyle>
#include <QPen>
#include <QPainter>

using namespace QPE;

QPropertyDelegate::QPropertyDelegate(QObject *parent) :
	QStyledItemDelegate(parent), RowHeight(0), AddRowHeight(false)
{
}

QPropertyDelegate::~QPropertyDelegate()
{
	//
}

QWidget* QPropertyDelegate::createEditor (QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	QWidget* tmpWidget = NULL;
	if(index.isValid())
	{
		QProperty* tmpProperty = reinterpret_cast<QProperty*>(index.internalPointer());
		tmpWidget = tmpProperty->createEditor(parent, option, this);
	}

	return tmpWidget ? tmpWidget : QStyledItemDelegate::createEditor(parent, option, index);
}


//! Sets the index data to the editor
void QPropertyDelegate::setEditorData (QWidget * editor, const QModelIndex & index) const
{
	bool done = false;
	if(index.isValid() && editor)
	{
		QProperty* tmpProperty = reinterpret_cast<QProperty*>(index.internalPointer());
		done = tmpProperty->setEditorData(editor);
	}

	if(!done)
		QStyledItemDelegate::setEditorData(editor, index);
}

//! Updates the index data
void QPropertyDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
	QVariant tmpData;
	if(index.isValid() && editor)
	{
		QProperty* tmpProperty = reinterpret_cast<QProperty*>(index.internalPointer());
		tmpData = tmpProperty->getEditorData(editor);
	}

	if(tmpData.isNull())
		QStyledItemDelegate::setModelData(editor, model, index);
	else
		model->setData(index, tmpData);
}

QSize QPropertyDelegate::sizeHint (const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	QSize tmpStandardSizeHint = QStyledItemDelegate::sizeHint(option, index);
	tmpStandardSizeHint.setHeight(tmpStandardSizeHint.height() + 1);

	if(RowHeight > 0)
		return QSize(tmpStandardSizeHint.width(), AddRowHeight ? tmpStandardSizeHint.height() + RowHeight : RowHeight);
	else
		return tmpStandardSizeHint;
}

void QPropertyDelegate::setRowHeight(int height, bool add_to_standard)
{
	RowHeight = height;
	AddRowHeight = add_to_standard;
}

void QPropertyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	bool done = false;
	if(index.isValid() && index.column() == 1)
		done = reinterpret_cast<QProperty*>(index.internalPointer())->paint(painter, option, index, this);

	if(!done)
		QStyledItemDelegate::paint(painter, option, index);

	QColor tmpPenColor = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));

	QPen tmpOldPen = painter->pen();
	painter->setPen(QPen(tmpPenColor));
	painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
	painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
	painter->setPen(tmpOldPen);
}



